package com.ruoyi.qiuyue.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户收藏对象 music_love
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
public class MusicLove extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 序列化对象 */
    private static ObjectMapper objectMapper = new ObjectMapper();

    /** $column.columnComment */
    private Long id;

    /** 用户id */
    @Excel(name = "用户id")
    private Long userId;

    /** 用户收藏的音乐id列表 Json { music_id,create_time,} */
    private String favorite;

    /** 将收藏反序列化获取 */
    public List<MusicFavorite> getFavoriteParse() throws JsonProcessingException {
        if (this.favorite == null) {
            return new ArrayList<>();
        }
        List<MusicFavorite> list = objectMapper.readValue(this.favorite, new TypeReference<List<MusicFavorite>>(){});
        return list;
    }

    /** 修改收藏并序列化 */
    public void setFavoriteParse(List<MusicFavorite> data) throws JsonProcessingException {
        if(data == null || data.size() == 0){
            this.favorite = "[]";
            return;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        this.favorite = objectMapper.writeValueAsString(data);
    }

    public void setId(Long id) 
    {
        this.id = id;
    }

    public Long getId() 
    {
        return id;
    }
    public void setUserId(Long userId)
    {
        this.userId = userId;
    }

    public Long getUserId()
    {
        return userId;
    }
    public void setFavorite(String favorite)
    {
        this.favorite = favorite;
    }

    public String getFavorite() 
    {
        return favorite;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("userId", getUserId())
            .append("favorite", getFavorite())
            .toString();
    }
}
